import java.util.Scanner;

public class ArrayHomework01 {
    public static void main(String[] args) {
        int[] arr = {10, 12, 45, 90};
        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.println("是否添加元素？y/n");
            String chooise = scanner.next();
            if (chooise.equals("y")) {
                int[] newArr = new int[arr.length + 1];
                System.out.println("请输入要插入的数据：");
                int element = scanner.nextInt();
                int index = -1;
                for (int i = 0; i < arr.length; i++) {
                    if (element <= arr[i]) {
                        index = i;
                        break;
                    }
                }
                if (index != -1) {
                    System.out.println("添加位置为：" + index);
                    newArr[index] = element;
                    for (int i = 0; i < index; i++) {
                        newArr[i] = arr[i];
                    }
                    for (int length = arr.length; length > index; length--) {
                        newArr[length] = arr[length - 1];
                    }
                }
                if (index == -1) {
                    index = newArr.length - 1;
                    System.out.println("添加位置为：" + index);
                    for (int i = 0; i < arr.length; i++) {
                        newArr[i] = arr[i];
                    }
                    newArr[index] = element;
                }
                arr = newArr;
                for (int i = 0; i < arr.length; i++) {
                    System.out.print(arr[i] + "\t");
                }
                System.out.println();
            } else {
                System.out.println("程序退出！");
                return;
            }
        }
    }
}
